@extends('layouts._app')
@section('content')
    <body>
    <div class="wrap-container">
        <div class="column-content-detail">
            <div class="col-lg-12">
                <div class="email-content" style="overflow:auto; width:100%;">
                    {!! Form::open(['class'=>'layui-form']) !!}
                    <div class="layui-form-item">
                        <div class="layui-input-inline tool-btn">
                            <a class="layui-btn layui-btn-small layui-btn-primary" id='email-refresh-btn'
                               href="{{route('expansion.job.index')}}?category={{$request['category']}}"
                               data-title='刷新'>
                                <i class="layui-icon">&#x1002;</i>
                            </a>
                            <button class="layui-btn layui-btn-small layui-btn-danger delBtn hidden-xs">
                                <i class="layui-icon">&#xe640;</i>
                            </button>
                        </div>
                        <div class="layui-input-inline">
                            <select lay-filter="status" lay-search>
                                <option value="-1" selected>全部</option>
                                <option value="1" @if($request['status'] == 1) selected @endif>正常</option>
                                <option value="2" @if($request['status'] == 2) selected @endif>禁用</option>
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <select lay-filter="static" lay-search>
                                <option value="-1" @if($request['static'] == -1) selected @endif>全部</option>
                                <option value="0" @if($request['static'] == 0) selected @endif>未执行</option>
                                <option value="1" @if($request['static'] == 1) selected @endif>执行成功</option>
                                <option value="2" @if($request['static'] == 2) selected @endif>执行失败</option>
                            </select>
                        </div>
                        <div class="layui-inline" style="float: right;">
                            <select id="change_category" lay-filter="category" lay-search
                                    data-id="failed_jobs">
                                <option selected>下载</option>
                                <option>.txt</option>
                                <option>.xlsx</option>
                            </select>
                        </div>
                    </div>
                    {!! Form::close() !!}
                    @include('layouts._flash')
                    <div class="layui-form" id="table-list">
                        <table class="layui-table">
                            <thead>
                            <tr>
                                <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                                <th>ID</th>
                                <th>队列KEY</th>
                                <th>内容</th>
                                <th>是否成功</th>
                                <th>是否禁用</th>
                                <th>失败次数</th>
                                <th>时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($job as $value)
                                <tr data-id="{{$value->id}}">
                                    <td><input type="checkbox" name="del" lay-skin="primary" data-id="{{$value->id}}">
                                    </td>
                                    <td title="{{$value->id}}">{{sub($value->id,20)}}</td>
                                    <td title="{{$value->key}}">{{sub($value->key,20)}}</td>
                                    <td title="{{$value->key}}" style="width: 100px;">{
                                        <br>@foreach($value->content as $key => $e)
                                            {{$key}} : @if(!is_array($e)) '{{$e}}',
                                            @else [ <br>@foreach($e as $k => $v)
                                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{$k}}
                                                    =>@if(!is_array($v)) {{$v}}
                                                    @else [<br>@foreach($v as $b=>$c)
                                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{$b}} => '{{$c}}'
                                                            <br>
                                                        @endforeach
                                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]
                                                    @endif
                                                    <br>
                                                @endforeach
                                                &nbsp;&nbsp;]
                                            @endif
                                            <br>
                                        @endforeach
                                        }
                                    </td>
                                    <td title="@if($value->static == 0) 未执行 @elseif($value->static == 1) 执行成功 @else 执行失败 @endif">
                                        @if($value->static == 0) 未执行 @elseif($value->static == 1) 执行成功 @else 执行失败 @endif
                                    </td>
                                    <td title="@if($value->status == 1) 正常 @else 禁用 @endif">
                                        @if($value->static == 1)
                                            @if($value->status == 1) 正常@else 禁用@endif
                                        @else
                                            <a class="layui-btn layui-btn-small layui-btn-warm times"
                                               href="javascript:void(0);"
                                               title="@if($value->status == 1) 正常@else 禁用@endif">
                                                <i class="@if($value->status == 1) fa fa-times @else fa fa-check @endif"></i></a>
                                        @endif
                                    </td>
                                    <td title="{{$value->count}}">{{sub($value->count,20)}}</td>
                                    <td title="{{$value->updated_at}}">{{sub($value->updated_at)}}</td>
                                    <td title="操作">
                                        @if($value->static != 1)
                                            <a class="layui-btn layui-btn-small layui-btn-normal check"
                                               href="javascript:void(0);" title="执行">
                                                <i class="fa fa-check"></i></a>
                                        @endif
                                        @if($value->static == 2)
                                            <a class="layui-btn layui-btn-small layui-btn-success put"
                                               href="javascript:void(0);" title="放入队列">
                                                <i class="fa fa-tasks"></i></a>
                                        @endif
                                        <a class="layui-btn layui-btn-small layui-btn-danger"
                                           href="{{route('expansion.job.destroy',$value->id)}}"
                                           data-method="delete" title="队列删除"
                                           data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                            <i class="layui-icon">&#xe640;</i></a>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                        <!--tp分页-->
                        <div class="page-wrap">
                            <p style="float: left;">注 : 共 {{$job->total()}} 条记录</p>
                            {{ $job->links() }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/admin/js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/destroy.js')}}" type="text/javascript" charset="utf-8"></script>
    <script>
        layui.use(['form', 'dialog'], function () {
            var form = layui.form();
            var dialog = layui.dialog;
            //顶部批量删除
            $('.delBtn').click(function () {
                var ids = [];
                $("input[name='del']:checked").each(function (i) {
                    ids[i] = $(this).data('id');
                });
                if (ids.length == 0) {
                    layer.msg('请选择要删除的', {icon: 5, time: 3000});
                    return false;
                }
                dialog.confirm({
                    message: '您确定要删除选中项',
                    success: function () {
                        $.ajax({
                            type: 'POST',
                            url: '{{route('expansion.job.delete')}}',
                            data: {id: ids, filter: true},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
                return false;

            }).mouseenter(function () {

                dialog.tips('批量删除', '.delBtn');

            })
            form.on('select(static)', function (e) {
                window.location.href = "{{route('expansion.job.index')}}?static=" + e.value;
            })
            form.on('select(status)', function (e) {
                window.location.href = "{{route('expansion.job.index')}}?status=" + e.value;
            })
            form.on('select(category)', function (e) {
                if (e.value == '下载') {
                    return false;
                }
                /**
                 * 写入文件下载
                 * @param id
                 */
                var table = $("#change_category").data('id');
                var ext = e.value;
                dialog.confirm({
                    message: '这可能会耗费很长时间！是否继续',
                    success: function () {
                        window.open('{{route('TextFile')}}' + '?table=' + table + '&ext=' + ext, '_parent');
                        window.location.reload();
                    },
                });
            });
            form.render();
            /**
             * 放入队列
             */
            $('.put').click(function () {
                var id = $(this).parents('tr').data('id');
                dialog.confirm({
                    message: '放入队列会清除现有数据，是否放入队列？',
                    success: function () {
                        $.ajax({
                            type: 'PATCH',
                            url: '{{route('expansion.job.status')}}',
                            data: {id: id, filter: true, status: 'put'},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
            })
            /**
             * 立即执行
             */
            $('.check').click(function () {
                var id = $(this).parents('tr').data('id');
                dialog.confirm({
                    message: '这可能会耗费很长时间！是否执行？',
                    success: function () {
                        $.ajax({
                            type: 'PATCH',
                            url: '{{route('expansion.job.status')}}',
                            data: {id: id, filter: true, status: 'check'},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
            })
            /**
             * 启用 or 禁用
             */
            $('.times').click(function () {
                var id = $(this).parents('tr').data('id');
                dialog.confirm({
                    message: '是否更改',
                    success: function () {
                        $.ajax({
                            type: 'PATCH',
                            url: '{{route('expansion.job.status')}}',
                            data: {id: id, filter: true, status: 'hide'},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
            })
        });
    </script>
    </body>
@endsection
